package com.jketing.entity.ams.base;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.jketing.annotation.FieldMeta;
import com.jketing.entity.BaseEntity;

/**
 * Organ 组织机构实体对象
 * 
 * @author  李荣华
 * @version v1.0
 * @date     2017-08-13
 * @since   Jpa 2.1
 */
@JsonIgnoreProperties(value= {"children","employees"})
@Entity
@Table(name = "Organ_")
public class Organ extends BaseEntity{
	private static final long serialVersionUID = -5583037525760670387L;
	/**
	 * parent:父级引用
	 */
	private Organ parent;
	/**
	 * region:行政区域
	 */
	@FieldMeta(name="Entity:region",explain="行政区域")
	private Region region;
	/**
	 * unitNature:单位性质
	 */
	@FieldMeta(name="Entity:unitNature",explain="单位性质")
	private Dictionary unitNature;
	/**
	 * subOrdination:隶属关系
	 */
	@FieldMeta(name="Entity:unitNature",explain="隶属关系")
	private Dictionary subOrdination;
	/**
	 * classifyToManage:分类管理类型
	 */
	@FieldMeta(name="Entity:unitNature",explain="分类管理类型")
	private Dictionary classifyToManage;
	/**
	 * type:机构类型
	 */
	private String type;
	/**
	 * typeName:类型名称
	 */
	private String typeName;
	/**
	 * name:机构名称
	 */
	@FieldMeta(name="String:name",explain="机构名称")
	private String name;
	/**
	 * grade:树型级别
	 */
	private Integer grade;
	/**
	 * formation:编制人数
	 */
	private Integer formation;
	/**
	 * tel:座机电话
	 */
	private String tel;	
	/**
	 * photo:照片
	 */
	private byte[] photo;
	/**
	 * longitude:经度
	 */
	private Double longitude;
	/**
	 * latitude:纬度
	 */
	private Double latitude;
	/**
	 * addr:机构地址
	 */
	private String addr;
	/**
	 * intro:机构简介
	 */
	private String intro;
	/**
	 * frontShow:前台展示
	 */
	private String frontShow;
	/**
	 * children:下级集合
	 */
	private Set<Organ> children = new HashSet<Organ>();
	/**
	 * employeeSet:员工集合
	 */
	private Set<Employee> employees = new HashSet<Employee>();
	
	/**
	 * region 所属行政区
	 */
	@ManyToOne
	@JoinColumn(name = "regionId")
	public Region getRegion() {
		return region;
	}
	public void setRegion(Region region) {
		this.region = region;
	}
	
	/**
	 * parent 由ParentId引用的上级部门对象
	 */
	@ManyToOne
	@JoinColumn(name = "ParentId")
	public Organ getParent() {
		return parent;
	}
	public void setParent(Organ parent) {
		this.parent = parent;
	}
	
	/**
	 * unitNature 单位性质：机关、事业、企业
	 */
	@ManyToOne
	@JoinColumn(name = "UnitNatureId")
	public Dictionary getUnitNature() {
		return unitNature;
	}
	public void setUnitNature(Dictionary unitNature) {
		//if(unitNature!=null && checkObjFieldIsNotNull(unitNature))
		this.unitNature = unitNature;
	}
	
	/**
	 * subOrdination 隶属关系:中央，省，市(地区)，县，街道，乡(镇),村（社区）
	 */
	@ManyToOne
	@JoinColumn(name = "subOrdinationId")
	public Dictionary getSubOrdination() {
		return subOrdination;
	}
	public void setSubOrdination(Dictionary subOrdination) {
		//if(subOrdination!=null && checkObjFieldIsNotNull(subOrdination))
		this.subOrdination = subOrdination;
	}
	
	/**
	 * classifyToManage 分类管理类型：非营利，营利，其它
	 */
	@ManyToOne
	@JoinColumn(name = "classifyToManageId")
	public Dictionary getClassifyToManage() {
		return classifyToManage;
	}
	public void setClassifyToManage(Dictionary classifyToManage) {
		//if(classifyToManage!=null && checkObjFieldIsNotNull(classifyToManage))
		this.classifyToManage = classifyToManage;
	}	

	/**
	 * type:机构类型
	 * 
	 * 取值 
	 * group:集团
	 * unit:单位
	 * branch:部门
	 */
	@Column(name="type_")
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	/**
	 * typeName:类型名称
	 * 
	 * 用于显示不影射数据库字段
	 */
	@Transient
	public String getTypeName() {
		if(StringUtils.isNotBlank(type)) {
			if("group".equals(type)) {
				typeName = "集团"; 
			}
			else if("unit".equals(type)){
				typeName = "单位";
			}else if("branch".equals(type)) {
				typeName = "部门";
			}
		}else {
			typeName = "未定";
		}
		return typeName;
	}
	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}
	
	/**
	 * name 机构全称
	 */
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	/**
	 *  grade 机构级别（在树形结构中的层次）
	 */
	@Column(name="grade_")
	public Integer getGrade() {
		return grade;
	}
	public void setGrade(Integer grade) {
		this.grade = grade;
	}
	
	/**
	 * formation 编制人数
	 */
	public Integer getFormation() {
		return formation;
	}
	public void setFormation(Integer formation) {
		this.formation = formation;
	}
	
	/**
	 * tel 组织机构座机电话
	 */
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	
	/**
	 * 照片
	 */
	@Lob
	public byte[] getPhoto() {
		return photo;
	}
	public void setPhoto(byte[] photo) {
		this.photo = photo;
	}
	
	/**
	 * longitude 经度，用于地图定位
	 */
	public Double getLongitude() {
		return longitude;
	}
	public void setLongitude(Double longitude) {
		this.longitude = longitude;
	}
	
	/**
	 * latitude 纬度，用于地图定位
	 */
	public Double getLatitude() {
		return latitude;
	}
	public void setLatitude(Double latitude) {
		this.latitude = latitude;
	}
	
	/**
	 * addr 组织机构地址
	 */
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	
	/**
	 * intro 组织机构简介
	 */
	@Column(columnDefinition="text")
	public String getIntro() {
		return intro;
	}
	public void setIntro(String intro) {
		this.intro = intro;
	}
	
	/**
	 * frontShow 前台展示   yes:是，no:否
	 */
	@Column(length=8)
	public String getFrontShow() {
		return frontShow;
	}
	public void setFrontShow(String frontShow) {
		this.frontShow = frontShow;
	}
	
	/**
	 * children 本机构下直接所属下级机构列表
	 */
	@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@OrderBy(value = "id ASC")
	public Set<Organ> getChildren() {
		return children;
	}
	public void setChildren(Set<Organ> children) {
		this.children = children;
	}	
	
	/**
	 * employees 本机构下的所有职工集合
	 */
	@ManyToMany(mappedBy = "organ", cascade = {CascadeType.REFRESH })
	public Set<Employee> getEmployees() {
		return employees;
	}
	public void setEmployees(Set<Employee> employees) {
		this.employees = employees;
	}
	
	/**
	 * 增加下级
	 * @param entity
	 */
	public void add(Organ entity) {
		if (!children.contains(entity)) {
			children.add(entity);
			entity.setParent(this);
		}
	}	

	/**
	 * 删除下级
	 * @param entity
	 */
	public void remove(Organ entity) {
		if (this.children.contains(entity)) {
			entity.setParent(null);
			this.children.remove(entity);
		}
	}
	
	/**
	 * 增加员工
	 * @param entity
	 */
	public void add(Employee entity){
		if (!employees.contains(entity)) {
			employees.add(entity);
		}
	}
	
	/**
	 * 删除员工
	 * @param entity
	 */
	public void remove(Employee entity){
		if (employees.contains(entity)) {
			employees.remove(entity);
		}
	}

	/**
	 * 	equals 判断实例是否相等
	 * 
	 * 	是本类实例并且实例id值相等返回true
	 * 	其它情况返回false
	 * 
	 * 	@param object 其它实例
	 */
	@Override
	public boolean equals(Object object) {
		if (!(object instanceof Organ)) {
			return false;
		}
		Organ other = (Organ) object;
		if(!id.equals(other.id)){
			return false;
		}
		return true;
	}
}
